#ifndef __ZHONG_CHAT_SESSION_MEMBER__
#define __ZHONG_CHAT_SESSION_MEMBER__
#include "Factory.hpp"
#include "chat_session_member.hxx"
#include "chat_session_member-odb.hxx"

namespace bite_im
{

    class ChatSessionMemberTable
    {
    public:
        using ptr = std::shared_ptr<ChatSessionMemberTable>;
        ChatSessionMemberTable(const DBPtr &db) : _db(db) {}
        // 会话的单个成员的新增
        bool append(ChatSessionMember &member)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());

                _db->persist(member);
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("向会话[{}]新增成员[{}]失败，原因[{}]", member.session_id(), member.user_id(), e.what());
                return false;
            }
            return true;
        }
        // 会话的多个成员的新增
        bool append(std::vector<ChatSessionMember> &member_list)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());

                for (auto &member : member_list)
                {
                    _db->persist(member);
                }
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("向会话[{}]新增多成员列表失败，原因[{}]", member_list[0].session_id(), e.what());
                return false;
            }
            return true;
        }
        // 删除指定会话的指定成员
        bool remove(ChatSessionMember &member)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<ChatSessionMember>;
                _db->erase_query<ChatSessionMember>(query::session_id == member.session_id() &&
                                                    query::user_id == member.user_id());
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("向会话[{}]删除成员[{}]失败，原因[{}]", member.session_id(), member.user_id(), e.what());
                return false;
            }
            return true;
        }
        // 删除整个会话
        bool remove(const std::string &session_id)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<ChatSessionMember>;
                _db->erase_query<ChatSessionMember>(query::session_id == session_id);
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("向会话[{}]删除所有成员失败，原因[{}]", session_id, e.what());
                return false;
            }
            return true;
        }
        std::vector<std::string> members(const std::string &session_id) // 直接传std::vector<std::string>引用作为参数可以减少拷贝 提高效率
        {
            std::vector<std::string> member_list;
            try
            {
                // 3.获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<ChatSessionMember>;
                using result = odb::result<ChatSessionMember>;
                // 组织sql语句--查询条件
               
                result ret(_db->query<ChatSessionMember>(query::session_id == session_id));
                for (auto it = ret.begin(); it != ret.end(); ++it)
                {
                    member_list.push_back(it->user_id());
                }
                // 4.提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("获取会话[{}]中的所有用户id失败，原因[{}]", session_id, e.what());
            }
            return member_list;
        }

    private:
        DBPtr _db;
    };
}

#endif